<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 小虎哥 <1105415366@qq.com>
 * Date: 2018-4-3
 */

namespace think\template\taglib\api;

use think\Db;
use app\home\logic\FieldLogic;

/**
 * 商品/文章列表(用于任何页面拿到部分商品/文章列表,不分页)
 */
class TagArclist extends Base
{
    public $fieldLogic;

    //初始化
    protected function _initialize()
    {
        parent::_initialize();
        $this->fieldLogic = new FieldLogic;
//        if ($this->aid > 0) { // 应用于文档列表
//            $this->tid = Db::name('archives')->where('aid', $this->aid)->getField('typeid');
//        }
    }

    /**
     * 获取列表
     * @author wengxianhu by 2018-4-20
     */
    public function getArclist($param = array())
    {
        //栏目ID
        $param['typeid'] = !empty($param['typeid']) ? $param['typeid'] : 0;
        //模型ID 优先级高于栏目ID,高于营销标签
        $param['channel'] = !empty($param['channel']) ? $param['channel'] : 0;
        //标题长度
        $param['titlelen'] = !empty($param['titlelen']) ? intval($param['titlelen']) : 100;
        //表示内容简介长度
        $param['infolen'] = !empty($param['infolen']) ? intval($param['infolen']) : 160;
        $param['orderby'] = !empty($param['orderby']) ? trim($param['orderby']) : '';
        //营销标签
        $param['label'] = !empty($param['label']) ? $param['label'] : 0;
        //列表里有商品是秒杀的.返回秒杀标识 users_price 会被秒杀价格覆盖
        $param['seckill'] = !empty($param['seckill']) ? $param['seckill'] : 'off';

        //自定义字段名
        $param['addfields'] = !empty($param['addfields']) ? str_replace(["'", '"'], '', $param['addfields']) : '';
        //显示作者用户信息
//        $arcrank = empty($param['arcrank']) ? 'off' : $param['arcrank'];

        if (!empty($param['limit'])) {
            $limit = !empty($param['limit']) ? str_replace('，', ',', $param['limit']) : 15;
            $limit = preg_replace('/[^\d\,]/i', '', $limit);
        } else {
            $limit = !empty($param['pagesize']) ? intval($param['pagesize']) : 15;
        }
        $param['limit'] = $limit;

        $param['orderway'] = !empty($param['orderway']) ? trim($param['orderway']) : 'desc';

        //如果栏目和模型都为空,那就默认为产品模型
        if (empty($param['typeid']) && empty($param['channel']) && empty($param['notypeid'])){
            $param['channel'] = 2;
        }elseif ( (!empty($param['typeid']) || !empty($param['notypeid'])) && empty($param['channel'])){
            if (!empty($param['typeid'])){
                $type_arr = explode(',',$param['typeid']);
            }else{
                $type_arr = explode(',',$param['notypeid']);
            }
            $param['channel'] = Db::name('arctype')->where('id',$type_arr[0])->value('current_channel');
        }

        //如果传了模型id和营销标签.不是产品模型的情况下优先模型
        if (!empty($param['channel']) && $param['channel'] != 2) {
            unset($param['label']);
        }

        // 如果指定了频道ID，则频道下的所有文档都展示
//        if (!empty($param['channel'])) { // 优先展示模型下的文章
//            unset($param['typeid']);
//        }
//        elseif (!empty($typeid)) { // 其次展示栏目下的文章
//            $typeidArr = explode(',', $typeid);
//            if (count($typeidArr) == 1) {
//                $typeid = intval($typeid);
//                $channel_info = Db::name('arctype')->field('id,current_channel')->where(array('id'=>$typeid))->find();
//                if (empty($channel_info)) {
//                    return false;
//                }
//                $channeltype = !empty($channel_info) ? $channel_info["current_channel"] : '';  // 当前栏目ID所属模型ID
//                /*当前模型ID不属于含有列表模型，直接返回无数据*/
//                if (false === array_search($channeltype, $allow_release_channel)) {
//                    return false;
//                }
//                /*end*/
//                /*获取当前栏目下的同模型所有子孙栏目*/
//                $arctype_list = model("Arctype")->getHasChildren($channel_info['id']);
//                foreach ($arctype_list as $key => $val) {
//                    if ($channeltype != $val['current_channel']) {
//                        unset($arctype_list[$key]);
//                    }
//                }
//                $typeids = get_arr_column($arctype_list, "id");
//                !in_array($typeid, $typeids) && $typeids[] = $typeid;
//                $typeid = implode(",", $typeids);
//                /*--end*/
//            } elseif (count($typeidArr) > 1) {
//                $firstTypeid = intval($typeidArr[0]);
//                $channeltype = Db::name('arctype')->where('id', $firstTypeid)->getField('current_channel');
//            }
//            $param['channel'] = $channeltype;
//        }
        //产品模型已经不使用这个标签了,使用label_types表的自定义标签

        if (!empty($param['label'])) {
            $redata = $this->getLabelList($param);
        } else {
            $redata = $this->getList($param);
        }
        return $redata;
    }

    private function getArctypeInfo($param)
    {
        $where = [];
        if (!empty($param['typeid'])) {
            $where['id'] = $param['typeid'];
        }
        if (!empty($param['channel'])) {
            $where['current_channel'] = $param['channel'];
        }
        $field = 'id,id as typeid,typename,current_channel';
        $result = Db::name('arctype')->field($field)
            ->where($where)
            ->order("id asc")
            ->find();

        return $result;
    }

    private function getLabelList($param)
    {
        //产品不使用这两个了 使用营销标签 label
        unset($param['flag']);
        unset($param['noflag']);
        // 查询条件
        $condition = array();
        foreach (array('keywords', 'keyword', 'typeid', 'notypeid', 'channel', 'label') as $key) {
            if (isset($param[$key]) && $param[$key] !== '') {
                if ($key == 'keyword') {
                    //单个关键词
                    eyou_add_search_word($param[$key]);
                    array_push($condition, "a.title LIKE '%{$param[$key]}%'");
                } elseif ($key == 'keywords' && !empty($param[$key])) {
                    //多个关键词
                    $keyword = str_replace('，', ',', $param[$key]);
                    $keywordArr = explode(',', $keyword);
                    $keywordArr = array_unique($keywordArr); // 去重
                    eyou_add_search_word($keywordArr);
                    foreach ($keywordArr as $_k => $_v) {
                        $_v = trim($_v);
                        if (empty($_v)) {
                            unset($keywordArr[$_k]);
                            break;
                        } else {
                            $keywordArr[$_k] = addslashes($_v);
                        }
                    }
                    $keyword = implode('|', $keywordArr);
                    $condition[] = Db::raw(" CONCAT(a.title,a.seo_keywords) REGEXP '$keyword' ");
                } elseif ($key == 'typeid') {
                    if (!empty($param[$key])) array_push($condition, "a.typeid IN ({$param[$key]})");

                } elseif ($key == 'notypeid') {
                    if (!empty($param[$key])) array_push($condition, "a.typeid NOT IN ({$param[$key]})");

                }  elseif ($key == 'label') {
                    //营销标签
                    array_push($condition, "c.types_id IN ({$param[$key]})");
                } else {
                    array_push($condition, "a.{$key} = '" . $param[$key] . "'");
                }
            }
        }

        array_push($condition, "a.arcrank > -1");
        array_push($condition, "a.status = 1");
        array_push($condition, "a.is_del = 0"); // 回收站功能

        $where_str = "";
        if (0 < count($condition)) {
            $where_str = implode(" AND ", $condition);
        }

        // 给排序字段加上表别名
        $orderby = getOrderBy($param['orderby'], $param['orderway'], true);

        // 获取查询的表名
        $channeltype_info = model('Channeltype')->getInfo($param['channel']);
        $controller_name = $channeltype_info['ctl_name'];
        $channeltype_table = $channeltype_info['table'];
        $channeltype_nid = $channeltype_info['nid'];

        $field = 'a.*,b.typename,c.types_id';
        if ('total_sales_num' == $param['orderby']){
            $field .= ',(a.virtual_sales + a.sales_num) as total_sales_num';
        }

        $result = Db::name('product_label')
            ->field($field)
            ->alias('c')
            ->join('archives a', 'a.aid = c.aid', 'LEFT')
            ->join('__ARCTYPE__ b', 'b.id = a.typeid', 'LEFT')
            ->where($where_str)
            ->orderRaw($orderby)
            ->limit($param['limit'])
            ->select();
        $arctypeInfo = $this->getArctypeInfo($param); //[];
        $aidArr = $adminArr = $usersArr = [];
        foreach ($result as $key => $val) {
            array_push($aidArr, $val['aid']);   // 收集文档ID
            array_push($adminArr, $val['admin_id']);    // 收集admin_id
            array_push($usersArr, $val['users_id']);    // 收集users_id
            $val['title'] = htmlspecialchars_decode($val['title']);
            $val['title'] = text_msubstr($val['title'], 0, $param['titlelen'], false);
            $val['seo_description'] = text_msubstr($val['seo_description'], 0, $param['infolen'], false);
            $val['seo_title'] = $this->set_arcseotitle($val['typename'], $val['seo_title']);
            $val['litpic'] = $this->get_default_pic($val['litpic']); // 默认封面图
            $val['add_time'] = $this->time_format($val['add_time']);
            $val['old_price'] = unifyPriceHandle($val['users_price']);
            $resultData = $this->handle_price($val['users_price']);
            $val['users_price'] = $resultData['users_price'];
            $val['level_discount'] = $resultData['level_discount'];
            $val['users_price_arr'] = explode('.', $val['users_price']);
            if ('on' == $param['seckill']){
                $val['is_seckill'] = 0;
            }
            $val['goods_sales'] = intval($val['virtual_sales']) + intval($val['sales_num']);
            $result[$key] = $val;
            array_push($aidArr, $val['aid']); // 文档ID数组

        }

        if ('on' == $param['seckill']) {
            //先判断当前有没有秒杀场次
            $current_time = date('Y-m-d H:00:00');
            $current_time = strtotime($current_time);
            $active_time_id = Db::name('sharp_active_time')->where('active_timestamp', $current_time)->value('active_time_id');
            if (!empty($active_time_id)) {
                $active_goods = Db::name('sharp_active_goods')
                    ->alias('a')
                    ->field('b.aid,b.seckill_price')
                    ->join('sharp_goods b', 'a.sharp_goods_id = b.sharp_goods_id', 'left')
                    ->where('a.active_time_id', $active_time_id)
                    ->where('b.status', 1)
                    ->where('b.is_del', 0)
                    ->where('b.seckill_stock', '>', 0)
                    ->where('b.aid', 'in', $aidArr)
                    ->getAllWithIndex('aid');
                if (!empty($active_goods)) {
                    foreach ($result as $key => $val) {
                        if (!empty($active_goods[$val['aid']])){
                            $val['users_price'] = $active_goods[$val['aid']]['seckill_price'];
                            $val['is_seckill'] = 1;
                        }
                        $result[$key] = $val;
                    }
                }
            }
        }

        $addfields = $param['addfields'];

        /*自定义字段*/
        if (!empty($addfields) && !empty($aidArr)) {
            $addtableName = $channeltype_table . '_content';
            $addfields = str_replace('，', ',', $addfields); // 替换中文逗号
            $addfields = trim($addfields, ',');
            /*过滤不相关的字段*/
            $addfields_arr = explode(',', $addfields);
            $extFields = Db::name($addtableName)->getTableFields();
            $addfields_arr = array_intersect($addfields_arr, $extFields);
            if (!empty($addfields_arr) && is_array($addfields_arr)) {
                $addfields = implode(',', $addfields_arr);
            } else {
                $addfields = '';
            }
            /*end*/
            if (!empty($addfields)) {
                $addfields = ',' . $addfields;
                if (strstr(",{$addfields},", ',content,')) {
                    $addfields .= ',content_ey_m';
                }
                $resultExt = M($addtableName)->field("aid {$addfields}")->where('aid', 'in', $aidArr)->getAllWithIndex('aid');
                /*自定义字段的数据格式处理*/
                $resultExt = $this->fieldLogic->getChannelFieldList($resultExt, $param['channel'], true);
                /*--end*/
                foreach ($result as $key => $val) {
                    $valExt = !empty($resultExt[$val['aid']]) ? $resultExt[$val['aid']] : array();
                    if (strstr(",{$addfields},", ',content,') && isset($valExt['content_ey_m'])) {
                        $valExt['content'] = $valExt['content_ey_m'];
                    }
                    if (isset($valExt['content_ey_m'])) {
                        unset($valExt['content_ey_m']);
                    }
                    if (!empty($valExt['content'])) {
                        $valExt['content_img_list'] = $this->get_content_img($valExt['content']);
                    }
                    $val = array_merge($valExt, $val);
                    $result[$key] = $val;
                }
            }

        }
        /*--end*/

        $redata = [
            'data' => !empty($result) ? $result : false,
        ];
        !empty($arctypeInfo) && $redata['arctype'] = $arctypeInfo;
        // cache($cacheKey, $redata, null, 'archives');
        return $redata;
    }

    private function getList($param)
    {
        // 查询条件
        $condition = array();
        foreach (array('keywords', 'keyword', 'typeid', 'notypeid', 'flag', 'noflag', 'channel') as $key) {
            if (isset($param[$key]) && $param[$key] !== '') {
                if ($key == 'keyword') {
                    //单个关键词
                    eyou_add_search_word($param[$key]);
                    array_push($condition, "a.title LIKE '%{$param[$key]}%'");
                } elseif ($key == 'keywords' && !empty($param[$key])) {
                    //多个关键词
                    $keyword = str_replace('，', ',', $param[$key]);
                    $keywordArr = explode(',', $keyword);
                    $keywordArr = array_unique($keywordArr); // 去重
                    eyou_add_search_word($keywordArr);
                    foreach ($keywordArr as $_k => $_v) {
                        $_v = trim($_v);
                        if (empty($_v)) {
                            unset($keywordArr[$_k]);
                            break;
                        } else {
                            $keywordArr[$_k] = addslashes($_v);
                        }
                    }
                    $keyword = implode('|', $keywordArr);
                    $condition[] = Db::raw(" CONCAT(a.title,a.seo_keywords) REGEXP '$keyword' ");
                } elseif ($key == 'typeid') {
                    if (!empty($param[$key])) array_push($condition, "a.typeid IN ({$param[$key]})");

                } elseif ($key == 'notypeid') {
                    if (!empty($param[$key])) array_push($condition, "a.typeid NOT IN ({$param[$key]})");

                } elseif ($key == 'flag') {
                    //产品模型已经不使用这个标签了,使用label_types表的自定义标签
                    $flag_arr = explode(",", $param[$key]);
                    $where_or_flag = array();
                    foreach ($flag_arr as $k2 => $v2) {
                        if ($v2 == "c") {
                            array_push($where_or_flag, "a.is_recom = 1");
                        } elseif ($v2 == "h") {
                            array_push($where_or_flag, "a.is_head = 1");
                        } elseif ($v2 == "a") {
                            array_push($where_or_flag, "a.is_special = 1");
                        } elseif ($v2 == "j") {
                            array_push($where_or_flag, "a.is_jump = 1");
                        } elseif ($v2 == "p") {
                            array_push($where_or_flag, "a.is_litpic = 1");
                        } elseif ($v2 == "b") {
                            array_push($where_or_flag, "a.is_b = 1");
                        } elseif ($v2 == "s") {
                            array_push($where_or_flag, "a.is_slide = 1");
                        } elseif ($v2 == "r") {
                            array_push($where_or_flag, "a.is_roll = 1");
                        } elseif ($v2 == "d") {
                            array_push($where_or_flag, "a.is_diyattr = 1");
                        }
                    }
                    if (!empty($where_or_flag)) {
                        $where_flag_str = " (" . implode(" OR ", $where_or_flag) . ") ";
                        array_push($condition, $where_flag_str);
                    }
                } elseif ($key == 'noflag') {
                    //产品模型已经不使用这个标签了,使用label_types表的自定义标签
                    $flag_arr = explode(",", $param[$key]);
                    $where_or_flag = array();
                    foreach ($flag_arr as $nk2 => $nv2) {
                        if ($nv2 == "c") {
                            array_push($where_or_flag, "a.is_recom <> 1");
                        } elseif ($nv2 == "h") {
                            array_push($where_or_flag, "a.is_head <> 1");
                        } elseif ($nv2 == "a") {
                            array_push($where_or_flag, "a.is_special <> 1");
                        } elseif ($nv2 == "j") {
                            array_push($where_or_flag, "a.is_jump <> 1");
                        } elseif ($nv2 == "p") {
                            array_push($where_or_flag, "a.is_litpic <> 1");
                        } elseif ($nv2 == "b") {
                            array_push($where_or_flag, "a.is_b <> 1");
                        } elseif ($nv2 == "s") {
                            array_push($where_or_flag, "a.is_slide <> 1");
                        } elseif ($nv2 == "r") {
                            array_push($where_or_flag, "a.is_roll <> 1");
                        } elseif ($nv2 == "d") {
                            array_push($where_or_flag, "a.is_diyattr <> 1");
                        }
                    }
                    if (!empty($where_or_flag)) {
                        $where_flag_str = " (" . implode(" OR ", $where_or_flag) . ") ";
                        array_push($condition, $where_flag_str);
                    }
                } else {
                    array_push($condition, "a.{$key} = '" . $param[$key] . "'");
                }
            }
        }

        array_push($condition, "a.arcrank > -1");
        array_push($condition, "a.status = 1");
        array_push($condition, "a.is_del = 0"); // 回收站功能

        $where_str = "";
        if (0 < count($condition)) {
            $where_str = implode(" AND ", $condition);
        }

        // 给排序字段加上表别名
        $orderby = getOrderBy($param['orderby'], $param['orderway'], true);

        // 获取查询的表名
        $channeltype_info = model('Channeltype')->getInfo($param['channel']);
        $controller_name = $channeltype_info['ctl_name'];
        $channeltype_table = $channeltype_info['table'];
        $channeltype_nid = $channeltype_info['nid'];

        $field = 'a.*,b.typename';
        if ('total_sales_num' == $param['orderby']){
            $field .= ',(a.virtual_sales + a.sales_num) as total_sales_num';
        }

        $result = Db::name('archives')
            ->field($field)
            ->alias('a')
            ->join('__ARCTYPE__ b', 'b.id = a.typeid', 'LEFT')
            ->where($where_str)
            ->orderRaw($orderby)
            ->limit($param['limit'])
            ->select();
        $arctypeInfo = $this->getArctypeInfo($param); //[];
        $aidArr = $adminArr = $usersArr = [];
        foreach ($result as $key => $val) {
            array_push($aidArr, $val['aid']);   // 收集文档ID
            array_push($adminArr, $val['admin_id']);    // 收集admin_id
            array_push($usersArr, $val['users_id']);    // 收集users_id
            $val['title'] = htmlspecialchars_decode($val['title']);
            $val['title'] = text_msubstr($val['title'], 0, $param['titlelen'], false);
            $val['seo_description'] = text_msubstr($val['seo_description'], 0, $param['infolen'], false);
            $val['seo_title'] = $this->set_arcseotitle($val['typename'], $val['seo_title']);
            $val['litpic'] = $this->get_default_pic($val['litpic']); // 默认封面图
            $val['add_time'] = $this->time_format($val['add_time']);
            $val['old_price'] = unifyPriceHandle($val['users_price']);
            $resultData = $this->handle_price($val['users_price']);
            $val['users_price'] = $resultData['users_price'];
            $val['level_discount'] = $resultData['level_discount'];
            $val['users_price_arr'] = explode('.', $val['users_price']);
            if ('on' == $param['seckill']){
                $val['is_seckill'] = 0;
            }
            $val['goods_sales'] = intval($val['virtual_sales']) + intval($val['sales_num']);
            $result[$key] = $val;

        }
        if ('on' == $param['seckill']) {
            //先判断当前有没有秒杀场次
            $current_time = date('Y-m-d H:00:00');
            $current_time = strtotime($current_time);
            $active_time_id = Db::name('sharp_active_time')->where('active_timestamp', $current_time)->value('active_time_id');
            if (!empty($active_time_id)) {
                $active_goods = Db::name('sharp_active_goods')
                    ->alias('a')
                    ->field('b.aid,b.seckill_price')
                    ->join('sharp_goods b', 'a.sharp_goods_id = b.sharp_goods_id', 'left')
                    ->where('a.active_time_id', $active_time_id)
                    ->where('b.status', 1)
                    ->where('b.is_del', 0)
                    ->where('b.seckill_stock', '>', 0)
                    ->where('b.aid', 'in', $aidArr)
                    ->getAllWithIndex('aid');
                if (!empty($active_goods)) {
                    foreach ($result as $key => $val) {
                        if (!empty($active_goods[$val['aid']])){
                            $val['users_price'] = $active_goods[$val['aid']]['seckill_price'];
                            $val['is_seckill'] = 1;
                        }
                        $result[$key] = $val;
                    }
                }
            }
        }
        //获取文章作者的信息 需要传值arcrank = on
//        if ('on' == $arcrank) {
//            $field = 'username,nickname,head_pic,users_id,admin_id,sex';
//            $userslist = Db::name('users')->field($field)
//                ->where('admin_id', 'in', $adminArr)
//                ->whereOr('users_id', 'in', $usersArr)
//                ->select();
//            foreach ($userslist as $key => $val) {
//                $val['head_pic'] = $this->get_head_pic($val['head_pic'], false, $val['sex']);
//                empty($val['nickname']) && $val['nickname'] = $val['username'];
//                if (!empty($val['admin_id'])) {
//                    $adminLitpicArr[$val['admin_id']] = $val;
//                }
//                if (!empty($val['users_id'])) {
//                    $usersLitpicArr[$val['users_id']] = $val;
//                }
//            }
//            $adminLitpic = Db::name('users')->field($field)->where('admin_id', '>', 0)->order('users_id asc')->find();
//            $adminLitpic['head_pic'] = $this->get_head_pic($adminLitpic['head_pic'], false, $adminLitpic['sex']);
//            empty($adminLitpic['nickname']) && $adminLitpic['nickname'] = $adminLitpic['username'];
//
//            foreach ($result as $key => $val) {
//                if (!empty($val['users_id'])) {
//                    $users = !empty($usersLitpicArr[$val['users_id']]) ? $usersLitpicArr[$val['users_id']] : [];
//                } elseif (!empty($val['admin_id'])) {
//                    $users = !empty($adminLitpicArr[$val['admin_id']]) ? $adminLitpicArr[$val['admin_id']] : [];
//                } else {
//                    $users = $adminLitpic;
//                }
//                !empty($users) && $val['users'] = $users;
//                $result[$key] = $val;
//            }
//        }

        $addfields = $param['addfields'];

        /*自定义字段*/
        if (5 == $param['channel']) {
            $addtableName = $channeltype_table . '_content';
            $addfields .= ',courseware,courseware_free,total_duration,total_video';
            $addfields = str_replace('，', ',', $addfields); // 替换中文逗号
            $addfields = trim($addfields, ',');
            /*过滤不相关的字段*/
            $addfields_arr = explode(',', $addfields);
            $addfields_arr = array_unique($addfields_arr);
            $extFields = Db::name($addtableName)->getTableFields();
            $addfields_arr = array_intersect($addfields_arr, $extFields);
            if (!empty($addfields_arr) && is_array($addfields_arr)) {
                $addfields = implode(',', $addfields_arr);
            } else {
                $addfields = '';
            }
            /*end*/
            !empty($addfields) && $addfields = ',' . $addfields;

            if (strstr(",{$addfields},", ',content,')) {
                $addfields .= ',content_ey_m';
            }
            $resultExt = M($addtableName)->field("aid {$addfields}")->where('aid', 'in', $aidArr)->getAllWithIndex('aid');
            /*自定义字段的数据格式处理*/
            $resultExt = $this->fieldLogic->getChannelFieldList($resultExt, $param['channel'], true);
            /*--end*/
            foreach ($result as $key => $val) {
                $valExt = !empty($resultExt[$val['aid']]) ? $resultExt[$val['aid']] : array();
                if (strstr(",{$addfields},", ',content,') && isset($valExt['content_ey_m'])) {
                    $valExt['content'] = $valExt['content_ey_m'];
                }
                if (isset($valExt['content_ey_m'])) {
                    unset($valExt['content_ey_m']);
                }
                if (!empty($valExt['content'])) {
                    $valExt['content_img_list'] = $this->get_content_img($valExt['content']);
                }
                $val = array_merge($valExt, $val);
                $val['total_duration'] = gmSecondFormat($val['total_duration'], ':');
                $result[$key] = $val;
            }
        } else
            if (!empty($addfields) && !empty($aidArr)) {
                $addtableName = $channeltype_table . '_content';
                $addfields = str_replace('，', ',', $addfields); // 替换中文逗号
                $addfields = trim($addfields, ',');
                /*过滤不相关的字段*/
                $addfields_arr = explode(',', $addfields);
                $extFields = Db::name($addtableName)->getTableFields();
                $addfields_arr = array_intersect($addfields_arr, $extFields);
                if (!empty($addfields_arr) && is_array($addfields_arr)) {
                    $addfields = implode(',', $addfields_arr);
                } else {
                    $addfields = '';
                }
                /*end*/
                if (!empty($addfields)) {
                    $addfields = ',' . $addfields;
                    if (strstr(",{$addfields},", ',content,')) {
                        if (in_array($param['channel'], [1, 2, 3, 4, 5, 6, 7])) {
                            $addfields .= ',content_ey_m';
                        } else {
                            if (in_array($extFields, ['content_ey_m'])) {
                                $addfields .= ',content_ey_m';
                            }
                        }
                    }
                    $resultExt = M($addtableName)->field("aid {$addfields}")->where('aid', 'in', $aidArr)->getAllWithIndex('aid');
                    /*自定义字段的数据格式处理*/
                    $resultExt = $this->fieldLogic->getChannelFieldList($resultExt, $param['channel'], true);
                    /*--end*/
                    foreach ($result as $key => $val) {
                        $valExt = !empty($resultExt[$val['aid']]) ? $resultExt[$val['aid']] : array();
                        if (strstr(",{$addfields},", ',content,') && isset($valExt['content_ey_m'])) {
                            $valExt['content'] = $valExt['content_ey_m'];
                        }
                        if (isset($valExt['content_ey_m'])) {
                            unset($valExt['content_ey_m']);
                        }
                        if (!empty($valExt['content'])) {
                            $valExt['content_img_list'] = $this->get_content_img($valExt['content']);
                        }
                        $val = array_merge($valExt, $val);
                        $result[$key] = $val;
                    }
                }
            }
        /*--end*/

        $redata = [
            'data' => !empty($result) ? $result : false,
        ];

        !empty($arctypeInfo) && $redata['arctype'] = $arctypeInfo;
        // cache($cacheKey, $redata, null, 'archives');
        return $redata;
    }

    private function handle_price($users_price)
    {
        $users = session('users');
        $result = [
            'level_discount' => 100,
            'users_price' => $users_price,
        ];
        if (!empty($users['level'])) {
            $level_discount = !empty($users['level_discount']) ? intval($users['level_discount']) : 100;
            $result['level_discount'] = intval($level_discount);
            if (!empty($level_discount) && 100 !== intval($level_discount)) {
                $level_discount = intval($level_discount) / intval(100);
                $users_price = floatval($users_price) * floatval($level_discount);
            }
        }
        $result['users_price'] = unifyPriceHandle($users_price);

        return $result;
    }
}